// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        player: {
            default: null,
            type: cc.Node
        },
        dici: {
            default: null,
            type: cc.Prefab,
        },
        timeLabel: {
            default: null,
            type: cc.Label,
        },
        scoreLabel: {
            default: null,
            type: cc.Label
        },
        bgAudio: {
            default: null,
            url: cc.AudioClip
        },
        jumpAudio: {
            default: null,
            url: cc.AudioClip
        },
        score: 0, // 分值
        playerJumpHeight: 30,
        wallWidth: 80,
        diciCount: 0,
        diciDuration: 140, // 地刺的间隔
        gameTime: 60, // 游戏的时间

    },

    // 向右移动
    playerMoveRight: function() {
        var goRight = cc.moveTo(0.2, cc.p(this.node.width / 2 - this.wallWidth, this.player.getPositionY()));
        var goR1 = cc.moveTo(0.1, cc.p(this.node.width / 2 - this.wallWidth - 30, this.player.getPositionY()));
        var goR2 = cc.moveTo(0.1, cc.p(this.node.width / 2 - this.wallWidth, this.player.getPositionY()));
        var sque = cc.sequence(goR1, goR2);

        // 如果是在右边，则小跳一下
        if (this.player.rotationY == 180) {
            this.player.runAction(sque);
        } else {
            this.player.runAction(goRight);
        }

        this.player.rotationY = 180;

    },

    // 向左方移动
    playerMoveLeft: function() {
        var goLeft = cc.moveTo(0.2, cc.p(-(this.node.width / 2 - this.wallWidth), this.player.getPositionY()));
        var goL1 = cc.moveTo(0.1, cc.p(-(this.node.width / 2 - this.wallWidth - 30), this.player.getPositionY()));
        var goL2 = cc.moveTo(0.1, cc.p(-(this.node.width / 2 - this.wallWidth), this.player.getPositionY()));
        var sque = cc.sequence(goL1, goL2);

        if (this.player.rotationY == 0) {
            this.player.runAction(sque);
        } else {
            this.player.runAction(goLeft);
        }
        this.player.rotationY = 0;
    },

    setInputControl: function() {
        var self = this;
        var listener = {
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function(touches, event) {
                cc.audioEngine.playEffect(self.jumpAudio, false);
                // 获得玩家输入点击是左边还是右边
                var target = event.getCurrentTarget();
                // 获取点击的坐标
                var locationInNode = target.convertToNodeSpace(touches.getLocation());
                // 判断是左边还是右边
                if (locationInNode.x > self.node.width / 2) {
                    // 点击屏幕的右边
                    self.playerMoveRight();
                } else {
                    // 点击屏幕左边
                    self.playerMoveLeft();
                }
                // 新增一个新的地刺
                self.newDici();
                self.score += 1;
                self.scoreLabel.string = self.score;
                cc.sys.localStorage.setItem("score", self.score);
                return true;
            },
            onTouchMoved: function(touches, event) {

            },
            onTouchEnded: function(touches, event) {

            }
        };
        cc.eventManager.addListener(listener, self.node);
    },

    newDici: function() {
        this.diciCount += 1;
        var newDici = cc.instantiate(this.dici);
        this.node.addChild(newDici);
        var randD = cc.random0To1();

        if (randD >= 0.5) { // 左边
            newDici.rotationY = 0;
        } else { // 右边
            newDici.rotationY = 180;
        }
        newDici.setPosition(this.diciPosition(randD));
    },

    diciPosition: function(randD) {
        var randX = 0;
        var randY = 0;
        if (randD >= 0.5) {
            randX = (this.node.width / 2 - this.wallWidth);
        } else {
            randX = -(this.node.width / 2 - this.wallWidth);
        }
        if (this.diciCount <= 8) {
            randY = this.node.height / 2 - this.diciDuration * (1 + this.diciCount)
        } else {
            randY = this.node.height / 2 - this.diciDuration * (1 + 8)
        }

        return cc.p(randX, randY);
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function() {
        this.diciCount = 0;
        cc.director.preloadScene("OverScene");
        cc.audioEngine.setEffectsVolume(0.2);
        cc.audioEngine.playMusic(this.bgAudio, true, 1.0);
        this.setInputControl();
        this.player.setPosition(-(this.node.width / 2 - this.wallWidth), this.node.height / 2 - 30)
        for (var i = 0; i < 8; i++) {
            this.newDici();
        }
        this.timeLabel.string = "倒计时:" + this.gameTime;
        this.schedule(function() {
            this.gameTime--;
            this.timeLabel.string = "倒计时:" + this.gameTime;
            if (this.gameTime <= 0) {
                cc.director.loadScene("OverScene");
            }
        }, 1)
    },

    start() {

    },

    // update (dt) {},
});